package com.chenyue.cm.user.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.common.utils.StringUtils;
import com.chenyue.cm.user.dao.JdGoodsSkuMapper;
import com.chenyue.cm.user.domain.JdGoodsSku;
import com.chenyue.cm.user.domain.JdGoodsSkuExample;
import com.chenyue.cm.user.service.JdGoodsSkuService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
*
* @author chenyue-cm
*/
@Service(value = "JdGoodsSkuService")
public class JdGoodsSkuServiceImpl extends BaseServiceImpl<JdGoodsSku>  implements JdGoodsSkuService {

private final JdGoodsSkuMapper jdGoodsSkuMapper;

	public JdGoodsSkuServiceImpl(@Qualifier("jdGoodsSkuMapper") JdGoodsSkuMapper jdGoodsSkuMapper) {
		this.jdGoodsSkuMapper = jdGoodsSkuMapper;
	}

	@Override
	@Resource(name = "jdGoodsSkuMapper")
	public void setBaseMapper(LcBaseMapper<JdGoodsSku, Serializable> baseMapper) {
		this.baseMapper = baseMapper;
	}

	@Override
	public Object getSale(String goodsId) {
		int num = 0; //定义销量
		List<JdGoodsSku> goodsSkuList = getMGoodsSkuByGoodsId(goodsId);
		if (goodsSkuList != null && goodsSkuList.size() > 0) {
			for (JdGoodsSku sku : goodsSkuList) {
				if (sku.getSkuSales() != null) {
					num += sku.getSkuSales();
				}
			}
		}
		return num;
	}

	@Override
	public Object getInventory(String goodsId) {
		int num = 0; //定义库存
		List<JdGoodsSku> goodsSkuList = getMGoodsSkuByGoodsId(goodsId);
		if (goodsSkuList != null && goodsSkuList.size() > 0) {
			for (JdGoodsSku sku : goodsSkuList) {
				num += sku.getSkuStock();
			}
		}
		return num;
	}

	@Override
	public BigDecimal getSkuGoodPriceByGoodsId(String goodsId) {
		return jdGoodsSkuMapper.getSkuMinPriceByGoodsId(goodsId);
	}

	@Override
	public BigDecimal getSkuMinPriceByGoodsId(String goodsId) {
		return jdGoodsSkuMapper.getSkuMinPriceByGoodsId(goodsId);
	}

	@Override
	public List<JdGoodsSku> getMGoods(String goodsId) {
		JdGoodsSkuExample aExample = new JdGoodsSkuExample();
		aExample.createCriteria().andGoodsIdEqualTo(goodsId);
		List<JdGoodsSku> mGoodsSkuList = jdGoodsSkuMapper.selectByExample(aExample);
		return mGoodsSkuList;
	}

	@Override
	public List<JdGoodsSku> getMGoodsSkuByGoodsId(String goodsId) {
		List<JdGoodsSku> mGoodsSkuList = jdGoodsSkuMapper.selectList(new LambdaQueryWrapper<JdGoodsSku>().eq(JdGoodsSku::getGoodsId,goodsId));
		return mGoodsSkuList;
	}

	@Override
	public Set<String> getVauleIds(String goodsId) {
		List<JdGoodsSku> mGoodsSkuByGoodsId = getMGoodsSkuByGoodsId(goodsId);  //根据商品id获取商品销售属性

		Set<String> setList = new HashSet<>();

		if (mGoodsSkuByGoodsId != null && mGoodsSkuByGoodsId.size() > 0) {
			for (JdGoodsSku mGoodsSku : mGoodsSkuByGoodsId) {
				String attrInfo = mGoodsSku.getAttrInfo();
				if (StringUtils.isEmpty(attrInfo)) {
					continue;
				}
				List<String> array = JSONObject.parseArray(attrInfo, String.class);
				for (String str : array) {
					setList.add(str);
				}
			}
		}
		return setList;
	}
}
